package com.personalblog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personalblog.dto.BlogQuery;
import com.personalblog.po.Blog;
import com.personalblog.po.Tag;
import com.personalblog.po.User;
import com.personalblog.service.BlogService;
import com.personalblog.service.TagService;
import com.personalblog.service.TypeService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/24 - 19:31
 */
@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TypeService typeService;

    @GetMapping("/blogs")
    public String blogs(@RequestParam(defaultValue = "1") int page, Model model){
        //根据时间降序
        String orderBy = "blog_update_time desc";
        //一页显示全部，不用分页
        PageHelper.startPage(page,500,orderBy);
        List<Blog> blogList = blogService.findBlogs();
        PageInfo pageInfo = new PageInfo(blogList);
        model.addAttribute("types",typeService.findTypes());
        model.addAttribute("pageInfo",pageInfo);
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(@RequestParam(defaultValue = "1") int page, BlogQuery blogQuery, Model model){
        //根据时间降序
        String orderBy = "blog_update_time desc";
        PageHelper.startPage(page,5,orderBy);
        List<Blog> blogList = blogService.findBlogsByBlogQuery(blogQuery);
        PageInfo pageInfo = new PageInfo(blogList);
        model.addAttribute("pageInfo",pageInfo);
//        return "admin/blogs";
        return "admin/blogs :: blogList";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("tags",tagService.findTags());
        model.addAttribute("types",typeService.findTypes());
        model.addAttribute("blog",new Blog());
        return "admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable int id, Model model){
        model.addAttribute("tags",tagService.findTags());
        model.addAttribute("types",typeService.findTypes());
        Blog blog = blogService.findBlogById(id);
        blog.init();
        model.addAttribute("blog",blog);
        return "admin/blogs-input";
    }

    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes redirectAttributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        //blog.setUserId(((User) session.getAttribute("user")).getUserId());
        //blog.setType(typeService.findTypeById(blog.getType().getTypeId()));
        //blog.setTypeId(blog.getType().getTypeId());
        //blog.setTagList(tagService.findTagListByIds(blog.getTagIds()));
        System.out.println("blog.getTagIds()======>" + blog.getTagIds());


        int i;
        if (blog.getBlogId() == null){
            i = blogService.insertBlog(blog);
        }else {
            i = blogService.updateBlog(blog.getBlogId(),blog);
        }


        if (i == 0){
            redirectAttributes.addFlashAttribute("message","操作失败");
        }else {
            redirectAttributes.addFlashAttribute("message","操作成功");
        }

        return "redirect:/admin/blogs";
    }


    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable int id,RedirectAttributes redirectAttributes){
        int i = blogService.deleteBlog(id);
        if (i == 0){
            redirectAttributes.addFlashAttribute("message","操作失败");
        }else {
            redirectAttributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admin/blogs";
    }


    @GetMapping("/blogsById/{id}")
    @ResponseBody
    public String blogsId(@PathVariable int id){

        return blogService.findBlogById(id).toString();
    }
    @GetMapping("/blogsCount")
    @ResponseBody
    public String blogsCount(){
        System.out.println(blogService.findBlogCount());

        return String.valueOf(blogService.findBlogCount());
    }

    @GetMapping("/blogList")
    @ResponseBody
    public String blogsList(){
        return blogService.findBlogs().toString();
    }
}
